local skel = fk.CreateSkill {
  name = "emo__xuewu",
}

Fk:loadTranslationTable{
  ["emo__xuewu"] = "血舞",
  [":emo__xuewu"] = "你可以将你与一名角色的各一张牌当【决斗】使用，若这两张牌：同色则伤害+1，同类型则响应方式改为打出一张此类型牌；均为你的牌，你因此受到伤害后摸两张牌；均不为以上，此技能本阶段无效。",

  ["#emo__xuewu"] = "血舞：将1-2张牌当【决斗】使用（若选1张，则还需选择其他角色的1张牌）",
  ["#emo__xuewu-choose"] = "血舞：选择一名角色，将其一张牌加入【决斗】底牌",
  ["#emo__xuewu-response"] = "血舞：你需打出一张 %arg，否则不能响应【决斗】",
}

skel:addEffect("viewas", {
  anim_type = "offensive",
  prompt = "#emo__xuewu",
  min_card_num = 1,
  max_card_num = 2,
  pattern = "duel",
  mute_card = false,
  card_filter = function (self, player, to_select, selected)
    return #selected < 2
  end,
  view_as = function (self, player, cards)
    if #cards == 0 then return nil end
    if #cards == 1 then
      if table.every(Fk:currentRoom().alive_players, function(p) return p == player or p:isNude() end) then
        return nil
      end
    end
    local card = Fk:cloneCard("duel")
    card.skillName = skel.name
    card:addSubcards(cards)
    return card
  end,
  before_use = function (self, player, use)
    local room = player.room
    local check = true
    use.extra_data = use.extra_data or {}
    if #use.card.subcards < 2 then
      local targets = table.filter(room.alive_players, function(p) return p ~= player and not p:isNude() end)
      if #targets == 0 then return "" end
      local to = player.room:askToChoosePlayers(player, {
        min_num = 1, max_num = 1, targets = targets, skill_name = skel.name,
        prompt = "#emo__xuewu-choose", cancelable = false,
      })[1]
      local cid = room:askToChooseCard(player, {target = to, flag = "he", skill_name = skel.name})
      use.card:addSubcard(cid)
    else
      use.extra_data.emo__xuewu_draw = player.id
      check = false
    end
    local subcards = use.card.subcards
    if #subcards ~= 2 then return "" end
    if Fk:getCardById(subcards[1]).color == Fk:getCardById(subcards[2]).color then
      use.additionalDamage = (use.additionalDamage or 0) + 1
      check = false
    end
    local t = Fk:getCardById(subcards[1]):getTypeString()
    if t == Fk:getCardById(subcards[2]):getTypeString() then
      use.extra_data.emo__xuewu_type = t
      check = false
    end
    if check then
      room:invalidateSkill(player, skel.name, "-phase")
    end
  end,
})

skel:addEffect(fk.AskForCardResponse, {
  mute = true,
  is_delay_effect = true,
  can_trigger = function (self, event, target, player, data)
    return target == player and data.eventData and data.eventData.extra_data and data.eventData.extra_data.emo__xuewu_type
    and not (data.extraData and data.extraData.emo__xuewu_draw_ask)
  end,
  on_cost = Util.TrueFunc,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local extra_data = table.simpleClone(data.extraData or {})
    extra_data.emo__xuewu_draw_ask = true -- 防止自嵌套询问
    local t = data.eventData.extra_data.emo__xuewu_type
    local respone = room:askToResponse(player, {
      pattern = ".|.|.|.|.|"..t, prompt = "#emo__xuewu-response:::" .. t,
      skill_name = skel.name,
      extra_data = extra_data,
      event_data = data.eventData,
    })
    if respone then
      data.result = respone
    else
      data.result = "__cancel"
    end
  end,
})

skel:addEffect(fk.Damaged, {
  mute = true,
  is_delay_effect = true,
  can_trigger = function (self, event, target, player, data)
    if target == player and data.card and not player.dead then
      local effectEvent = player.room.logic:getCurrentEvent():findParent(GameEvent.CardEffect)
      if effectEvent then
        local effect = effectEvent.data
        return effect.card == data.card and effect.extra_data and effect.extra_data.emo__xuewu_draw == player.id
      end
    end
  end,
  on_cost = Util.TrueFunc,
  on_use = function(self, event, target, player, data)
    player:drawCards(2, skel.name)
  end,
})

return skel
